export function base64UrlEncode(str) {
  var encodedSource = CryptoJS.enc.Base64.stringify(str);
  var reg = new RegExp('/', 'g');
  encodedSource = encodedSource.replace(/=+$/,'').replace(/\+/g,'-').replace(reg,'_');
  return encodedSource;
}

export function generateToken(secretSalt, userId, userTokenName) {
 let header = JSON.stringify({
 "alg": "HS256",
 "typ": "JWT"
 })

 var iat = new Date().getTime()
 var exp = iat + 2*60*60*1000
 console.log(exp-iat);
 let payload =JSON.stringify({
  //  "opendId": "123456",
  //  "iat": iat,
  //  "exp": exp,
  "userId": `'${userId}'`,
  "userTokenName": `'${userTokenName}'`
 })

 let before_sign = base64UrlEncode(CryptoJS.enc.Utf8.parse(header)) + '.' + base64UrlEncode(CryptoJS.enc.Utf8.parse(payload));
 let signature =CryptoJS.HmacSHA256(before_sign, secretSalt);
 signature = base64UrlEncode(signature);
 let final_sign = before_sign + '.' + signature;
 return final_sign;
}